//
//  EFSTopBannerManager.m
//  EFSMobile
//
//  Created by Chuan on 12/3/15.
//  Copyright © 2015 Elephants Financial Service. All rights reserved.
//

#import "EFSTopBannerManager.h"

//#import "YSUserManager.h"

//#import "NSUserDefaults+YS.h"
//#import <YTXUtilCategory/NSArray+YTXUtilCategory.h>
//
//#import <YTXRequest/YTXRequest.h>

static NSString *const EFSTopBannerNSUserDefaultKey = @"EFSTopBannerNSUserDefaultKey";

@implementation EFSTopBannerManager

+ (instancetype)sharedManager {
    static EFSTopBannerManager *sharedEFSTopBannerManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedEFSTopBannerManager = [[self alloc] init];
    });
    return sharedEFSTopBannerManager;
}

- (RACSignal *)fetchTopBanner {
    RACSubject *signal = [RACSubject subject];
    

//    YSRequest *request =  [YSRequest requestWithName:@"activity.banner"];
//    request.requestSerializerType = YSRequestSerializerTypeJSON;
//    
//    NSDictionary *params = @{@"serverId":@"3004",
//                             @"userTypes":@"0",
//                             @"navigationTag":@"154"};
//
//    @weakify(self);
//    [request sendWithParameters:params success:^(id response) {
//        @strongify(self);
//        NSLog(request.requestUrl, request.requestMethod, params, response, nil);
//        NSArray *latestList = [self filterAvailableBannerImage:response[@"articles"]];
//        [signal sendNext:latestList];
//        [signal sendCompleted];
//    } failure:^(YSRequest *request) {
//        NSError *erorr = [NSError errorWithDomain:@"EFS" code:0 userInfo:@{EFS_ERROR_MESSAGE_KEY: @"首页banner信息获取失败"}];
//        NSLog(request.requestUrl, request.requestMethod, params, nil, erorr);
//        [signal sendError:nil];
//    }];
    

    return signal;
}

- (NSArray *)filterAvailableBannerImage:(NSArray *)result
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *dict in result) {
        if (![dict[@"img"] isEqual:[NSNull null]] && [dict[@"img"] isKindOfClass:[NSString class]] && [(NSString *)dict[@"img"] rangeOfString:@"htt"].length > 0) {
            [array addObject:dict];
        }
    }
    return [array copy];
}

#pragma 读写缓存
- (void)saveTopBanner: (nullable NSArray *) data {
//    [[NSUserDefaults userDefaultWithGroup] setObject:[data toJSONString] forKey:EFSTopBannerNSUserDefaultKey];
//    [[NSUserDefaults userDefaultWithGroup] synchronize];
}

- (nonnull NSArray *)loadTopBanner {
//    return [(NSString *)[[NSUserDefaults userDefaultWithGroup] objectForKey:EFSTopBannerNSUserDefaultKey] jsonStringToArray];
    return nil;
}

@end
